package cn.edu.sjtu.se.dslab.component.design.generate;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import cn.edu.sjtu.se.dslab.component.data.DomainEntity;
import cn.edu.sjtu.se.dslab.component.data.util.DataComponentUtil;
import cn.edu.sjtu.se.dslab.component.dp.parse.DesignPatternInstanceBean;
import cn.edu.sjtu.se.dslab.project.BusZone;
import cn.edu.sjtu.se.dslab.project.DisplayZone;
import cn.edu.sjtu.se.dslab.project.EntityZone;
import cn.edu.sjtu.se.dslab.project.Project;
import cn.edu.sjtu.se.dslab.service.IDisplayZoneService;
import cn.edu.sjtu.se.dslab.service.IDomainEntityService;
import cn.edu.sjtu.se.dslab.util.FileUtil;

@Service("displayZoneGenerator")
public class DisplayZoneGenerator extends DesignPatternCodeGenerator {

	private static final Logger logger = Logger.getLogger(DisplayZoneGenerator.class);

	private static final String DISPLAY_ZONE_C_TEMPLATE_NAME = "display_data_zone_c.vm";
	private static final String DISPLAY_ZONE_H_TEMPLATE_NAME = "display_data_zone_h.vm";
	private static final String DISPLAY_ZONE_H_FILE_NAME = "display_data_zone.h";

	@Resource(name = "displayZoneService")
	IDisplayZoneService displayZoneService;

	@Resource(name = "domainEntityService")
	IDomainEntityService domainEntityService;

	@Resource(name = "dataComponentUtil")
	DataComponentUtil dataComponentUtil;

	public DisplayZoneGenerator() {
		
	}

	@Override
	public void generateCode(String targetDirPath, Project project,
			DesignPatternInstanceBean... dpiBean) throws IOException {
		
		String absoluteTargetDirPath = FileUtil.getRootPath() + targetDirPath;
		
		Map<String, DisplayZone> displayZoneMap = new HashMap<String, DisplayZone>();
		Set<String> entitySets = new HashSet<String>();
		Set<Long> entityIds = new HashSet<Long>();
		List<String> dependFileList = new ArrayList<String>();	//所有的依赖文件
		
		for (DisplayZone displayZone : displayZoneService.getDisplayZoneByProject(project.getId())){
			if (displayZone.getEntityUri() == null || displayZone.getEntityUri().equals(""))
				continue;
			DomainEntity domainEntity = domainEntityService
					.getDomainEntityByUri(displayZone.getEntityUri());
			if (domainEntity == null) continue;
			entityIds.add(domainEntity.getId());
			entitySets.add(domainEntity.getName());
			displayZoneMap.put(domainEntity.getName(), displayZone);
		}
		
		
		for(String s: dataComponentUtil.getDependences(new ArrayList<Long>(entityIds))){
			String tmp = s.substring(s.lastIndexOf('/')+1,	s.length());
			dependFileList.add(tmp);
			logger.info("DisplayZone :"  + tmp);
			FileUtil.copyFile(s, absoluteTargetDirPath + "App/", null);// 把依赖的文件复制到生成代码的文件夹下
		}
		
		inputData("displayZoneMap", displayZoneMap);
		inputData("entitySets", entitySets);

		StringBuilder displayZoneHeaderFile = new StringBuilder(targetDirPath);
		displayZoneHeaderFile.append("/App/").append(DISPLAY_ZONE_H_FILE_NAME);
		generateCodeFile(displayZoneHeaderFile.toString(),
				DISPLAY_ZONE_H_TEMPLATE_NAME);


	}

}
